package com.example.a79273.shandianban.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.SearchActivity;
import com.example.a79273.shandianban.adapter.MyAlphaPageTransformer;
import com.example.a79273.shandianban.adapter.YouHuo_PagerAdapter;
import com.example.a79273.shandianban.config.NetconfigYouHuo;
import com.squareup.picasso.Picasso;
import com.zhy.magicviewpager.transformer.AlphaPageTransformer;
import com.zhy.magicviewpager.transformer.ScaleInTransformer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 79273 on 2016/8/29.
 */
public class YouHuo extends Fragment implements View.OnClickListener {
    private View view;
    private View mViewTablayout;
    private TextView mTitle;
    private ImageView mImageViewSearch;
    private ImageView mImageViewTopPic;
    private RequestParams mParamsTopPic;
    private String pic_scr;//上面图片的地址
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;
    private Fragment mFragmentSon;
    private LinearLayout mLinearLayout;
    private ImageView[] mDot;
    private String[] mPaths = {NetconfigYouHuo.LATEST_PATH,NetconfigYouHuo.HOT_PATH,NetconfigYouHuo.FEMALE_PATH,NetconfigYouHuo.OTHERS_PATH};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_youhuo,null);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mImageViewSearch.setOnClickListener(this);
        //viewPager的监听事件
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < mDot.length; i++) {
                    mDot[i].setEnabled(true);
                }
                mDot[position].setEnabled(false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void setData() {
        //设置viewPager页面之间的间距
        mViewPager.setPageMargin(20);
        //设置缓存的页面数量
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setPageTransformer(true,new AlphaPageTransformer(new ScaleInTransformer()));
    }

    private void initData() {
        //--------------------获取上面的图片的地址
        mParamsTopPic = new RequestParams(NetconfigYouHuo.TOP_PIC_PATH);
        x.http().get(mParamsTopPic, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if( !TextUtils.isEmpty(result)) {
                    try {
                        JSONArray array = new JSONArray(result);
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("bannerlist");
                            for (int j = 0; j < jsonArray.length(); j++) {
                                JSONObject jsonObject1 = jsonArray.getJSONObject(j);
                                pic_scr = jsonObject1.getString("pic_scr");//得到图片的地址
                                //Log.i("TAG","---图片地址"+pic_scr);
                            }
                        }
                        Picasso.with(getActivity()).load(NetconfigYouHuo.PIN_JIE+pic_scr).into(mImageViewTopPic);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络出小差了...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        mFragments = new ArrayList<>();
        mAdapter = new YouHuo_PagerAdapter(getActivity().getSupportFragmentManager(),mFragments);
        //创建下面的viewPager
        for (int i = 0; i < 4; i++) {
            mFragmentSon = new YouHuoSonFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("count",i);
            bundle.putString("path",mPaths[i]);
            mFragmentSon.setArguments(bundle);
            mFragments.add(mFragmentSon);
        }
        mAdapter.notifyDataSetChanged();

        //下面的小圆点
        mDot = new ImageView[mFragments.size()];
        for (int i = 0; i < mDot.length; i++) {
            mDot[i] = (ImageView) mLinearLayout.getChildAt(i);
            mDot[i].setEnabled(true);
        }
        mDot[0].setEnabled(false);



    }

    private void initView() {
        mViewTablayout = (View) view.findViewById(R.id.in_youhuo_tablayout);
        mTitle = (TextView) mViewTablayout.findViewById(R.id.tv_tablayout_title);
        mTitle.setText("有货");
        mImageViewSearch = (ImageView) mViewTablayout.findViewById(R.id.iv_tablayout_search);
        mImageViewTopPic = (ImageView) view.findViewById(R.id.iv_youhuo_topPic);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_youhuo_body);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.ll_youhuo_dot);
    }

    //所有的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.iv_tablayout_search://跳转到搜索界面
                startActivity(new Intent(getActivity(),SearchActivity.class));
                break;
        }
    }
}
